package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;


public class Consumer {
    public static void main(String[] args) throws Exception {
        /*连接*/
        ConnectionFactory factory = new ConnectionFactory();
        //factory.setHost("192.168.64.140");
        factory.setHost("wht6.cn");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        /*创建随机队列*/
        String queue = UUID.randomUUID().toString();
        channel.queueDeclare(queue, false, true, true, null);

        /*创建交换机*/
        channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);

        /*将交换机和队列进行绑定*/
        channel.queueBind(queue, "logs", "");

        /*收到消息后,用来处理消息的回调对象*/
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String s = new String(message.getBody());
                System.out.println("收到: "+s);
            }
        };

        /*消费者取消时的回调对象*/
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };

        /*从队列接收消息*/
        channel.basicConsume(queue, false, deliverCallback, cancelCallback);
    }
}
